// pages/doctor/test/test.js
Page({
  data: {
    testResults: []
  },

  onLoad() {
    this.runTests();
  },

  // 运行测试
  runTests() {
    const tests = [
      {
        name: '医生模块数据加载测试',
        test: () => this.testDataLoading()
      },
      {
        name: '患者信息管理测试',
        test: () => this.testPatientManagement()
      },
      {
        name: '医生档案管理测试',
        test: () => this.testDoctorProfile()
      },
      {
        name: '消息系统测试',
        test: () => this.testMessageSystem()
      }
    ];

    const results = [];
    tests.forEach(test => {
      try {
        const result = test.test();
        results.push({
          name: test.name,
          status: 'success',
          message: result
        });
      } catch (error) {
        results.push({
          name: test.name,
          status: 'error',
          message: error.message
        });
      }
    });

    this.setData({ testResults: results });
  },

  // 测试数据加载
  testDataLoading() {
    // 模拟数据加载
    const mockData = {
      patients: [
        { id: 1, name: '张小明', age: 28, healthStatus: '良好' },
        { id: 2, name: '李小红', age: 35, healthStatus: '需关注' }
      ],
      messages: [
        { id: 1, patientName: '张小明', unreadCount: 2 },
        { id: 2, patientName: '李小红', unreadCount: 0 }
      ]
    };

    if (mockData.patients.length === 2 && mockData.messages.length === 2) {
      return '数据加载成功，患者数量：' + mockData.patients.length;
    } else {
      throw new Error('数据加载失败');
    }
  },

  // 测试患者管理
  testPatientManagement() {
    const patient = {
      id: 1,
      name: '张小明',
      healthMetrics: {
        bmi: 22.5,
        bloodPressure: { systolic: 120, diastolic: 80 },
        bloodSugar: { fasting: 5.8 }
      }
    };

    if (patient.healthMetrics.bmi && patient.healthMetrics.bloodPressure.systolic) {
      return '患者健康指标正常，BMI：' + patient.healthMetrics.bmi;
    } else {
      throw new Error('患者健康指标缺失');
    }
  },

  // 测试医生档案
  testDoctorProfile() {
    const profile = {
      name: '王医生',
      title: '主任医师',
      department: '营养科',
      specialties: ['糖尿病管理', '高血压饮食']
    };

    if (profile.name && profile.title && profile.specialties.length > 0) {
      return '医生档案完整，专长：' + profile.specialties.join('、');
    } else {
      throw new Error('医生档案不完整');
    }
  },

  // 测试消息系统
  testMessageSystem() {
    const messages = [
      { id: 1, content: '医生，我最近血糖有点高', sender: 'patient' },
      { id: 2, content: '建议您调整饮食', sender: 'doctor' }
    ];

    const unreadCount = messages.filter(m => m.sender === 'patient').length;
    
    if (messages.length === 2 && unreadCount === 1) {
      return '消息系统正常，未读消息：' + unreadCount;
    } else {
      throw new Error('消息系统异常');
    }
  },

  // 跳转到医生工作台
  goToDoctor() {
    wx.navigateTo({
      url: '/pages/doctor/doctor'
    });
  },

  // 跳转到患者详情
  goToPatientDetail() {
    wx.navigateTo({
      url: '/pages/doctor/patientDetail/patientDetail?id=1'
    });
  },

  // 跳转到聊天页面
  goToChat() {
    wx.navigateTo({
      url: '/pages/doctor/chat/chat?patientId=1'
    });
  }
})
